<template>
  <div class="app_main">
    <div class="search_box">
      <el-form :inline="true" :model="searchForm" size="mini" ref="searchForm">
        <search-form-box>
          <template slot="showSearch">
            <el-form-item label="项目：" prop="customer_id">
              <el-select v-model="searchForm.customer_id" filterable clearable placeholder="请选择项目"
                         style="width: 240px"
              >
                <el-option v-for="item in customer_arr" :key="item.id" :value="item.id" :label="item.name"/>
              </el-select>
            </el-form-item>
            <el-form-item label="甲方" prop="part_a">
              <el-input v-model="searchForm.part_a" placeholder="请输入甲方名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="合同编号" prop="contract_num">
              <el-input v-model="searchForm.contract_num" placeholder="请输入合同编号" clearable></el-input>
            </el-form-item>
            <el-form-item label="合同时间" prop="contract_begin_date">
              <el-date-picker
                v-model="searchForm.contract_begin_date"
                type="date"
                placeholder="开始时间"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="至" prop="contract_end_date">
              <el-date-picker
                v-model="searchForm.contract_end_date"
                type="date"
                placeholder="结束时间"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </template>
          <template slot="hideSearch">
            <el-form-item label="付款时间" prop="sign_date_begin">
              <el-date-picker
                v-model="searchForm.sign_date_begin"
                type="date"
                placeholder="开始时间"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="至" prop="sign_date_end">
              <el-date-picker
                v-model="searchForm.sign_date_end"
                type="date"
                placeholder="结束时间"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="是否置换" prop="mode">
              <el-select v-model="searchForm.is_replace" placeholder="请选择是否置换" clearable>
                <el-option value="0" label="否"></el-option>
                <el-option value="1" label="是"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="是否开票" prop="mode">
              <el-select v-model="searchForm.is_invoice" placeholder="请选择是否开票" clearable>
                <el-option value="0" label="否"></el-option>
                <el-option value="1" label="是"></el-option>
              </el-select>
            </el-form-item>
          </template>
          <template slot="searchBtn">
            <el-form-item>
              <el-button size="mini" type="primary" plain @click="search">查询</el-button>
              <el-button size="mini" type="info" plain @click="resetForm('searchForm')">重置</el-button>
            </el-form-item>
          </template>
        </search-form-box>
      </el-form>
    </div>
    <!--    <el-row style="margin-bottom: 10px">-->
    <!--      <el-button @click="createContract" v-permission="'create_contract'" size="mini" type="primary">新增合同-->
    <!--      </el-button>-->
    <!--    </el-row>-->
    <div class="data_box">
      <el-table :data="tableData" border style="width: 100%" v-loading="tableLoading" size="mini" :max-height="tableMaxHeight">
        <el-table-column fixed="left" width="50" label="序号" align="center">
          <template slot-scope="scope">
            <span>{{ (listParams.page - 1) * listParams.page_size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="甲方" width="100" prop="part_a" align="center" fixed="left"></el-table-column>
<!--        <el-table-column label="合同编号" width="120" prop="contract_num" align="center" fixed="left"></el-table-column>-->
        <el-table-column label="乙方名称" width="200" prop="part_b_name" align="center"></el-table-column>
        <el-table-column label="合同编号(鼠标悬停可查看资源明细)" width="120" align="center" fixed="left">
          <template slot-scope="scope">
            <el-popover
              placement="top"
              width="200"
              trigger="hover"
              :open-delay="300"
            >
              <div>
                <el-card>
                  <div slot="header" class="clearfix">
                    <span>自有资源</span>
                  </div>
                  <div v-if="scope.row.contractMediaPriceList && scope.row.contractMediaPriceList.length">
                    <div
                      v-for="(item,index) in scope.row.contractMediaPriceList" :key="index"
                    >
                      {{ item.media_type_name + ':' + item.amount + '元' }}
                    </div>
                  </div>
                  <div v-else>
                    暂无
                  </div>
                </el-card>
                <el-card style="margin-top: 10px">
                  <div slot="header" class="clearfix">
                    <span>代理资源</span>
                  </div>
                  <div v-if="scope.row.contractMediaPriceProxyList && scope.row.contractMediaPriceProxyList.length">
                    <div
                      v-for="(item,index) in scope.row.contractMediaPriceProxyList" :key="index"
                    >
                      {{ item.media_type_name + ':' + item.amount + '元' }}
                    </div>
                  </div>
                  <div v-else>
                    暂无
                  </div>
                </el-card>
              </div>
              <div slot="reference">{{ scope.row.contract_num }}</div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" width="100" align="center" fixed="left">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.flow_status == 3" size="mini">流程中</el-tag>
            <el-tag v-if="scope.row.flow_status == 4" type="success" size="mini">已通过</el-tag>
            <el-tag v-if="scope.row.flow_status == 0" type="info" size="mini">草稿</el-tag>
            <el-tag v-if="scope.row.flow_status == 2" type="danger" size="mini">被退回</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="合同时间" width="180" align="center">
          <template slot-scope="scope">
            <el-tag type="primary" size="mini">{{ scope.row.contract_begin_date + '~' + scope.row.contract_end_date }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column label="优惠价格" width="120" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.discounted_price }}元</span>
          </template>
        </el-table-column>

        <el-table-column label="已执行金额" width="120" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.totalExecutedAmount ? scope.row.totalExecutedAmount : 0 }}元</span>
          </template>
        </el-table-column>

        <el-table-column label="未执行金额" width="120" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.discounted_price > scope.row.totalExecutedAmount ? (scope.row.discounted_price - scope.row.totalExecutedAmount) : 0}}元</span>
          </template>
        </el-table-column>

        <el-table-column label="媒体单价（自营）" width="180" align="center">
          <template slot-scope="scope">
        	  <div class="flex justify-center align-center flex-direction">
        		  <el-tag size="mini" v-for="item in scope.row.mediaCountList">
        		    {{ item.name }}: {{item.num}}个,单价:{{item.price}}元
        		  </el-tag>
        	  </div>
          </template>
        </el-table-column>
        <el-table-column label="媒体单价（代理）" width="180" align="center">
          <template slot-scope="scope">
        	  <div class="flex justify-center align-center flex-direction">
        		  <el-tag size="mini" v-for="item in scope.row.proxyMediaCountList">
        		    {{ item.name }}: {{item.num}}个,单价:{{item.price}}元
        		  </el-tag>
        	  </div>
          </template>
        </el-table-column>
<!--        <el-table-column label="大写" width="120" prop="discounted_price_big" align="center"></el-table-column>-->
        <el-table-column label="更换画面价格" width="120" prop="replace_price" align="center"></el-table-column>
        <el-table-column label="广告发布费" width="120" prop="advertising_costs" align="center"></el-table-column>
<!--        <el-table-column label="大写" width="120" prop="advertising_costs_big" align="center"></el-table-column>-->
<!--        <el-table-column label="甲方联系方式" width="120" prop="part_a_contact" align="center"></el-table-column>-->
<!--        <el-table-column label="乙方联系人" width="120" prop="part_b_contact" align="center"></el-table-column>-->
<!--        <el-table-column label="乙方联系方式" width="100" prop="part_b_phone" align="center"></el-table-column>-->
        <el-table-column label="其他约定" prop="memo" align="center" min-width="200px" show-overflow-tooltip
        ></el-table-column>
        <el-table-column fixed="right" label="操作" width="240" align="center">
          <template slot-scope="scope">
            <el-button-group>
              <el-button style="margin-left: 0!important;" type="primary" size="mini"
                         @click="look(scope.row.last_contract_id || scope.row.id)"
              >查看
              </el-button>
              <el-button v-permission="'look_supplementary_agreement'" type="success" size="mini"
                         @click="lookSupplementaryContract(scope.row.id)"
              >查看补充协议
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="searchForm.page"
      :page-sizes="[10, 20, 50, 100, 200, 400]"
      :page-size.sync="searchForm.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 10px"
    >
    </el-pagination>

    <el-dialog title="查看补充协议" :visible.sync="lookDialogVisible" width="70%" center @close="closeLookContractDialog">
      <el-table :data="lookTableData" border v-loading="lookTableLoading" size="mini">
        <el-table-column fixed="left" width="50" label="序号" align="center">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="甲方" min-width="100" prop="part_a" align="center" fixed="left"></el-table-column>
        <el-table-column label="合同编号" width="120" prop="contract_num" align="center" fixed="left"></el-table-column>
        <el-table-column label="甲方联系方式" width="100" prop="part_a_phone" align="center" fixed="left"></el-table-column>
        <el-table-column label="审核状态" width="100" align="center" fixed="left">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.flow_status == 3" size="mini">流程中</el-tag>
            <el-tag v-if="scope.row.flow_status == 4" type="success" size="mini">已通过</el-tag>
            <el-tag v-if="scope.row.flow_status == 0" type="info" size="mini">草稿</el-tag>
            <el-tag v-if="scope.row.flow_status == 2" type="danger" size="mini">被退回</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="合同时间" width="180" align="center">
          <template slot-scope="scope">
            <el-tag type="primary" size="mini">{{ scope.row.contract_begin_date + '~' + scope.row.contract_end_date }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="签订日期" width="120" prop="sign_date" align="center"></el-table-column>
        <el-table-column label="优惠价格" width="120" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.discounted_price }}元</span>
          </template>
        </el-table-column>
        <el-table-column label="大写" width="120" prop="discounted_price_big" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" width="80" align="center">
          <template slot-scope="scope">
            <el-button-group>
              <el-button style="margin-left: 0!important;" type="primary" size="mini"
                         @click="toSupplementaryAgreementLookPage(scope.row.id)"
              >查看
              </el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="lookDialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import SearchFormBox from '@/components/SearchFormBox'
import { list, list_amend_contract, listByRootId } from '@/api/contract'
import { company_list_all } from '@/api/customer/customer'
import { generateUUID } from '@/utils/util'

export default {
  name: 'list',
  components: { SearchFormBox },
  data() {
    return {
      tableMaxHeight: 0,
      tableData: [],
      tableLoading: false,
      searchForm: {
        page: 1,
        page_size: 10,
        // 甲方
        part_a: null,
        // 甲方联系方式
        part_a_phone: null,
        // 乙方联系方式
        part_b_phone: null,
        // 合同开始日期
        contract_begin_date: null,
        // 合同结束日期
        contract_end_date: null,
        // 签订日期-开始
        sign_date_begin: null,
        // 签订日期-结束
        sign_date_end: null,
        // 其他约定
        memo: null,
        mode: null,
        contract_num: null,
        customer_id: null,
      },
      listParams: {
        page: 1,
        page_size: 10,
        // 甲方
        part_a: null,
        // 甲方联系方式
        part_a_phone: null,
        // 乙方联系方式
        part_b_phone: null,
        // 合同开始日期
        contract_begin_date: null,
        // 合同结束日期
        contract_end_date: null,
        // 签订日期-开始
        sign_date_begin: null,
        // 签订日期-结束
        sign_date_end: null,
        // 其他约定
        memo: null,
        mode: null,
        contract_num: null,
        customer_id: null,
      },
      total: null,
      lookTableLoading: false,
      lookTableData: [],
      lookDialogVisible: false,
      customer_arr: [],
    }
  },
  created() {
    this.tableMaxHeight = (document.documentElement.clientHeight || document.body.clientHeight) - 210
    this.getList()
    this.initCustomerArr()
  },
  methods: {
    handleSizeChange(val) {
      this.listParams.page_size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listParams.page = val
      this.getList()
    },
    initCustomerArr() {
      company_list_all({}).then(res => {
        this.customer_arr = res.data
      })
    },
    getList() {
      this.tableLoading = true
      list(this.listParams).then(res => {
        this.tableLoading = false
        this.total = res.data.total
        this.tableData = res.data.records
      }).catch(() => {
        this.tableLoading = false
      })
    },
    search() {
      this.searchForm.page = 1
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    look(id) {
      // 进入合同页
      const uuid = generateUUID()
      this.$store.commit('keep/PUT_PAGE_PARAMS', {
        uuid: uuid,
        data: {
          id: id
        }
      })
      this.$router.push({
        name: 'look_contract_detail',
        query: {
          uuid: uuid
        }
      })
    },
    /**
     * 查看补充协议合同
     * @param id
     */
    lookSupplementaryContract(id) {
      this.lookDialogVisible = true
      this.lookTableLoading = true
      listByRootId({ rootContractId: id }).then(res => {
        this.lookTableData = res.data
        this.lookTableLoading = false
      })
    },
    // 查看补充协议
    toSupplementaryAgreementLookPage(id) {
      let uuid = generateUUID()
      this.$store.commit('keep/PUT_PAGE_PARAMS', {
        uuid: uuid,
        data: {
          id,
          is_update: true
        }
      })
      this.$router.push({
        name: 'list_supplementary_agreement_look_page',
        query: {
          uuid
        }
      })
    },
    closeLookContractDialog() {
      this.lookTableData = []
    }
  }
}
</script>

<style lang="scss">
.app_main {
  padding: 20px 20px 0 20px;
}
</style>
